package at.meder.evaluation.editors;



import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISaveablePart;
import org.eclipse.ui.ISaveablePart2;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import at.meder.evaluation.View;
import at.meder.evaluation.model.CriteriaProvider;
import at.meder.evaluation.model.Criterion;

public class CostsEditor extends EditorPart implements ISaveablePart  {

	public static final String ID = "at.meder.evaluation.editor.CostsEditor";
	
	private Criterion criterion;
	private EditorInput input;
	private boolean dirty=false;

	private Text metric1;
	private Text metric2;
	private Text metric3;

	
	
	public CostsEditor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		
		criterion.setMetric1(metric1.getText());
		criterion.setMetric2(metric2.getText());
		criterion.setMetric3(metric3.getText());
		//connection.setPort(port.getText());
		
		evaluateSaved();
		
		
		View view = (View) getSite().getPage().findView(View.ID);
		ListViewer viewer = view.getViewer();
		viewer.refresh();
		dirty = false;
	    firePropertyChange(ISaveablePart.PROP_DIRTY);

	}
	

	

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		
		displayLevel();
	}

	private void displayLevel() {
		MessageDialog.openInformation(getSite().getShell(), "Evaluation level", "Level: "+criterion.getLevel());
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		
		if (!(input instanceof EditorInput)) {
			throw new RuntimeException(Messages.CostsEditor_1);
		}

		EditorInput new_name = (EditorInput) input;
		this.input = (EditorInput) input;
		setSite(site);
		setInput(input);
		criterion = CriteriaProvider.getInstance().getCriterionById(this.input.getId());
		setPartName(Messages.CostsEditor_2 + criterion.getName());


	}

	@Override
	public boolean isDirty() {
		return dirty;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		parent.setLayout(layout);
		
		Label label1 = new Label(parent, SWT.BORDER);
		label1.setText(criterion.getMetric1_name());
		
		
		
		
		
		
		metric1 = new Text(parent, SWT.BORDER);
		metric1.setText(criterion.getMetric1());
		metric1.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		Label label2 = new Label(parent, SWT.BORDER);
		label2.setText(criterion.getMetric2_name());
		metric2 = new Text(parent, SWT.BORDER);
		metric2.setText(criterion.getMetric2());
		metric2.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		
		Label label3 = new Label(parent, SWT.BORDER);
		label3.setText(criterion.getMetric3_name());
		metric3 = new Text(parent, SWT.BORDER);
		metric3.setText(criterion.getMetric3());
		metric3.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		
		
		
		
		Button button2 = new Button(parent, SWT.PUSH);
		button2.setText(Messages.CostsEditor_3);
		button2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				
				//ISelection selection = getSite().getWorkbenchWindow().getSelectionService().getSelection(View.ID);
				View view = (View) getSite().getPage().findView(View.ID);
				ListViewer viewer = view.getViewer();
				evaluate();
				
				
				
				viewer.refresh();
				//List<Criterion> criteria = CriteriaProvider.getInstance().getCriteria();
				//criteria.add(criterion);
				
				
			}

			
			
		});
				
				
				
				
				

		
		metric1.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                setDirty(true);
            }
        });
		metric2.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                setDirty(true);
            }
        });
		metric3.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                setDirty(true);
            }
        });

		

	}

	private void evaluate() {
		try{
			if(metric1.getText()!=""){
				int m1 = Integer.valueOf(metric1.getText());
				if(m1==0){
					criterion.setLevel(1);
					
					if(metric2.getText()!=""){
						int m2 = Integer.valueOf(metric2.getText());
						if(metric3.getText()!=""){
							int m3 = Integer.valueOf(metric3.getText());
							if(m2==0 && m3==0){
								criterion.setLevel(2);
							}
							else{
								criterion.setLevel(1);
							}
						
						}
					
					}
				}
				else{
					criterion.setLevel(0);
				}
			}
			else{
				criterion.setLevel(0);
			}
		}
		catch(NumberFormatException ex){
			criterion.setLevel(0);
		}
		displayLevel();
		
	}
	private void evaluateSaved() {
		
		
		try{
			if(criterion.getMetric1()!=""){
				int m1 = Integer.valueOf(criterion.getMetric1());
				if(m1==0){
					criterion.setLevel(1);
					
					if(criterion.getMetric2()!=""){
						int m2 = Integer.valueOf(criterion.getMetric2());
						if(criterion.getMetric3()!=""){
							int m3 = Integer.valueOf(criterion.getMetric3());
							if(m2==0 && m3==0){
								criterion.setLevel(2);
							}
							else{
								criterion.setLevel(1);
							}
						
						}
					
					}
				}
				else{
					criterion.setLevel(0);
				}
			}
			else{
				criterion.setLevel(0);
			}
		}
		catch(NumberFormatException ex){
			criterion.setLevel(0);
		}
		displayLevel();
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
		firePropertyChange(IEditorPart.PROP_DIRTY);

	}
/*
	@Override
	public int promptToSaveOnClose() {
		View view = (View) getSite().getPage().findView(View.ID);
		ListViewer viewer = view.getViewer();
		evaluateSaved();
		viewer.refresh();
		return 0;
	}
*/
	

	
	
	
}
